package com.obas.business.element.service.impl;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.ArrayUtils;

import com.ease.dao.statement.factory.NamedStatementFactory;
import com.ease.platform.access.service.impl.AbstractNamespaceJdbcTemplateAccessServiceImpl;
import com.obas.access.business.service.CircuitAccessService;
import com.obas.access.business.service.CustomerAccessService;
import com.obas.access.business.service.CustomerNetworkServingAccessService;
import com.obas.access.business.service.DeviceAccessService;
import com.obas.business.element.service.NetworkTopologyManagementService;
import com.obas.business.element.vo.NetworkTopologyParam;
import com.obas.model.business.Circuit;
import com.obas.model.business.CustomerNetworkServing;
import com.obas.model.business.Device;
import com.obas.model.metadata.constant.DictionaryConst;

public class NetworkTopologyManagementServiceImpl extends AbstractNamespaceJdbcTemplateAccessServiceImpl implements NetworkTopologyManagementService {

	private CustomerAccessService customerAccessService;

	private CustomerNetworkServingAccessService customerNetworkServingAccessService;

	private DeviceAccessService deviceAccessService;

	private CircuitAccessService circuitAccessService;

	// TODO
	public void optimizeRedudence() {
		// A-----D
		// A-B-C-D
		// remove A-D的link

	}

	public NetworkTopologyParam viewTopology(long deviceId) {

		NetworkTopologyParam networkTopologyParam = new NetworkTopologyParam();

		Set<Long> deviceIds = new HashSet<Long>();
		deviceIds.add(deviceId);
		//
		List<Circuit> circuits = circuitAccessService.findLinkRecuisiveByUpperDeviceId(deviceId);

		if (notEmpty(circuits)) {
			networkTopologyParam.setCircuits(circuits);
			for (Circuit circuit : circuits) {
				deviceId = circuit.getAendDeviceId();
				if (deviceId > 0) {
					deviceIds.add(deviceId);
				}
				deviceId = circuit.getZendDeviceId();
				if (deviceId > 0) {
					deviceIds.add(deviceId);
				}
			}
		}
		//

		List<Device> devices = deviceAccessService.findByIds(ArrayUtils.toPrimitive(deviceIds.toArray(new Long[0])));
		networkTopologyParam.setDevices(devices);

		return networkTopologyParam;
	}

	public NetworkTopologyParam viewRoute(long customerId) {

		CustomerNetworkServing customerNetworkServing = customerNetworkServingAccessService.findByCustomerIdNetworkServingType(customerId, DictionaryConst.AaaCustomer_ServiceType.ADSL);
		if (null == customerNetworkServing) {
			return null;
		}

		//
		long deviceId = customerNetworkServing.getZendDeviceId();

		NetworkTopologyParam networkTopologyParam = new NetworkTopologyParam();
		Set<Long> deviceIds = new HashSet<Long>();
		deviceIds.add(deviceId);

		List<Circuit> circuits = circuitAccessService.findLinkRecuisiveByLowerDeviceId(deviceId);

		if (notEmpty(circuits)) {
			networkTopologyParam.setCircuits(circuits);
			for (Circuit circuit : circuits) {
				deviceId = circuit.getAendDeviceId();
				if (deviceId > 0) {
					deviceIds.add(deviceId);
				}
				deviceId = circuit.getZendDeviceId();
				if (deviceId > 0) {
					deviceIds.add(deviceId);
				}
			}
		}
		//

		List<Device> devices = deviceAccessService.findByIds(ArrayUtils.toPrimitive(deviceIds.toArray(new Long[0])));
		networkTopologyParam.setDevices(devices);
		return networkTopologyParam;
	}

	/**
	 * @return the deviceAccessService
	 */
	public DeviceAccessService getDeviceAccessService() {
		return deviceAccessService;
	}

	/**
	 * @param deviceAccessService
	 *            the deviceAccessService to set
	 */
	public void setDeviceAccessService(DeviceAccessService deviceAccessService) {
		this.deviceAccessService = deviceAccessService;
	}

	/**
	 * @return the circuitAccessService
	 */
	public CircuitAccessService getCircuitAccessService() {
		return circuitAccessService;
	}

	/**
	 * @param circuitAccessService
	 *            the circuitAccessService to set
	 */
	public void setCircuitAccessService(CircuitAccessService circuitAccessService) {
		this.circuitAccessService = circuitAccessService;
	}

	/**
	 * @return the namedStatementFactory
	 */
	public NamedStatementFactory getNamedStatementFactory() {
		return namedStatementFactory;
	}

	/**
	 * @param namedStatementFactory
	 *            the namedStatementFactory to set
	 */
	public void setNamedStatementFactory(NamedStatementFactory namedStatementFactory) {
		this.namedStatementFactory = namedStatementFactory;
	}

	/**
	 * @return the customerAccessService
	 */
	public CustomerAccessService getCustomerAccessService() {
		return customerAccessService;
	}

	/**
	 * @param customerAccessService
	 *            the customerAccessService to set
	 */
	public void setCustomerAccessService(CustomerAccessService customerAccessService) {
		this.customerAccessService = customerAccessService;
	}

	/**
	 * @return the customerNetworkServingAccessService
	 */
	public CustomerNetworkServingAccessService getCustomerNetworkServingAccessService() {
		return customerNetworkServingAccessService;
	}

	/**
	 * @param customerNetworkServingAccessService the customerNetworkServingAccessService to set
	 */
	public void setCustomerNetworkServingAccessService(CustomerNetworkServingAccessService customerNetworkServingAccessService) {
		this.customerNetworkServingAccessService = customerNetworkServingAccessService;
	}

}
